package org.skydiveapphd;

import org.skydiveapphd.ui.FragmentHelper;
import org.skydiveapplibrary.R;
import org.skydiveapplibrary.data.MoreCategory;
import org.skydiveapplibrary.ui.MoreCategoryAdapter;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class MoreActivity extends BaseListActivity
{
	// listeners
	private OnItemClickListener itemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id)
		{
			// get selected category
			MoreCategoryAdapter adapter = (MoreCategoryAdapter)getListAdapter();
			MoreCategory category = (MoreCategory)adapter.getItem(position);
	    	// handle selection
			handleSelection(category);
		}
	};
	
	@Override 
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		// set adapter
		setListAdapter(new MoreCategoryAdapter(this));
		
		// set header
		setHeaderText(R.string.main_more_tab_label);
		
		getListView().setOnItemClickListener(itemClickListener);
		
		// hide both buttons
		setTopButtonVisibility(View.GONE);
		setBottomButtonVisibility(View.GONE);
	}
	
	private void handleSelection(MoreCategory category)
	{
		if (category == MoreCategory.Aircraft)
		{	
			FragmentHelper.getInstance().openFragment(new AircraftListActivity(), android.R.id.tabhost, 
					Constants.TAG_AIRCRAFT_LIST, getParent().getFragmentManager());
		}
		else if (category == MoreCategory.SkydiveTypes)
		{
			FragmentHelper.getInstance().openFragment(new SkydiveTypeListActivity(), android.R.id.tabhost, 
					Constants.TAG_SKYDIVE_TYPE_LIST, getParent().getFragmentManager());
		}
		else if (category == MoreCategory.Locations)
		{
			FragmentHelper.getInstance().openFragment(new LocationListActivity(), android.R.id.tabhost, 
					Constants.TAG_LOCATION_LIST, getParent().getFragmentManager());
		}
		else if (category == MoreCategory.WingLoadCalculator)
		{
			FragmentHelper.getInstance().openFragment(new WingLoadCalculatorActivity(), android.R.id.tabhost, 
					Constants.TAG_WING_LOAD_CALCULATOR, getParent().getFragmentManager());
		}
		else if (category == MoreCategory.ImportExport)
		{
			FragmentHelper.getInstance().openFragment(new ImportExportActivity(), android.R.id.tabhost, 
					Constants.TAG_IMPORT_EXPORT, getParent().getFragmentManager());
		}
		else if (category == MoreCategory.Settings)
		{
			FragmentHelper.getInstance().openFragment(new SettingsActivity(), android.R.id.tabhost, 
					Constants.TAG_SETTINGS, getParent().getFragmentManager());
		}
	}
}
